<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class controller_statistics extends lib_controller
{
	private $u_id;
	private $operations;
	private $role;
	private $roles = array();
	
	public function init($action)
	{
		$this->operations = lib_toolkit::getOperations();
		$_session_ = $this->_session_;
		$this->role = $_session_->getSession('u_role');
		$this->u_id = $_session_->getSession('u_id');
		$this->roles = explode(',',$this->role);
		
		if($action == 'showCalendarPage' || $action == 'doAjaxSearch' || $action == 'showStatisticsPage')
		{
			if(!in_array('doJobStatistics',$this->operations))
			{
				die("Permission denied.");
			}
		}
		
		lib_toolkit::filterBrowserScript();
	}
	
	public function showCalendarPage()
	{
		$this->_layout = 'calendar';
		$_mysql_ = $this->_mysql_;
		$templates = array();
		$u_id = $this->u_id;
		$users = array();
		$res_user = array();
		
		if(in_array('master', $this->roles) || in_array('admin', $this->roles))//admin和master可以看到所有模板情况
		{
			$templates = $_mysql_->findAll("templates","tpl_status = 6 OR tpl_status = 8");
		}
		else //其他人只可以看到自己模板情况
		{
			$templates = $_mysql_->findAll("templates","(tpl_status = 6 OR tpl_status = 8) AND (tpl_designer_id = $u_id OR tpl_master_id = $u_id OR tpl_tester_leader_id = $u_id OR tpl_tester_id = $u_id OR tpl_publisher_id = $u_id)");
		}
		
		$res_user = $_mysql_->findAll('users');
		foreach($res_user as $v)
		{
			$users[$v['u_id']] = $v['u_name'];
		}
		
		if(empty($templates) || empty($res_user))
		{
			$this->assign('isEmpty',true);
		}
		else
		{
			$this->assign('isEmpty',false);
			$this->assign('users', $users);
			$this->assign('templates', $templates);
			$this->assign('users',$users);
		}
	}
	
	public function showStatisticsPage()
	{
		$_mysql_ = $this->_mysql_;
		$users = array();
		$designers = array();
		$testers = array();
		$template_categories = array();
		$template_status = array();
		$tmp = array();
		
		$users = $_mysql_->findAll('users');
		$template_categories = $_mysql_->findAll('template_categories');
		$template_status = $_mysql_->findAll('status');
		
		if(empty($users) || empty($template_categories) || empty($template_status))
		{
			$this->assign('isEmpty',true);
		}
		else
		{
			$this->assign('isEmpty',false);
		}
		
		foreach($users as $k => $v)
		{
			$tmp = explode(",",$v['u_role']);
			if(in_array('designer',$tmp))
			{
				$designers[] = $v;
			}
			
			if(in_array('tester',$tmp))
			{
				$testers[] = $v;
			}
		}
		
		$this->assign('designers',$designers);
		$this->assign('testers',$testers);
		$this->assign('template_categories',$template_categories);
		$this->assign('template_status',$template_status);
	}
	
	public function doSearchJob()
	{
		$this->_layout = 'response';
		$_mysql_ = $this->_mysql_;
		$condition = ''; 
		$tpl_category_id = trim($_GET['tpl_category_id']);
    	$tpl_designer_id = trim($_GET['tpl_designer_id']);
    	$tpl_tester_id = trim($_GET['tpl_tester_id']);
    	$tpl_status = trim($_GET['tpl_status']);
    	$tpl_start_time = trim($_GET['tpl_start_time']);
    	if(!empty($tpl_start_time)) $tpl_start_time = strtotime($tpl_start_time);
    	$tpl_end_time =  trim($_GET['tpl_end_time']);
    	if(!empty($tpl_end_time)) $tpl_end_time = strtotime($tpl_end_time);
		
    	if(!empty($tpl_category_id)) $condition .= " AND t.tpl_category_id = $tpl_category_id";
    	if(!empty($tpl_designer_id)) $condition .= " AND t.tpl_designer_id = $tpl_designer_id";
    	if(!empty($tpl_tester_id)) $condition .= " AND t.tpl_tester_id = $tpl_tester_id";
    	if(!empty($tpl_status)) $condition .= " AND t.tpl_status = $tpl_status";
    	if(!empty($tpl_start_time)) $condition .= " AND tpl_assign_time >= $tpl_start_time";
    	if(!empty($tpl_end_time)) $condition .= " AND tpl_finish_time <= $tpl_end_time";
    	
    	$sql = <<<STR
SELECT tpl_id,tpl_name,tpl_domain,s_alias,tpl_category_name,designer_name,u1.u_name AS tester_name
FROM
(
SELECT t.tpl_id,t.tpl_name,t.tpl_domain,s.s_alias,tc.tpl_category_name,u.u_name AS designer_name,tpl_tester_id
FROM tpl_templates t
INNER JOIN tpl_status s ON t.tpl_status = s.s_id
INNER JOIN tpl_template_categories tc ON t.tpl_category_id = tc.tpl_category_id
LEFT JOIN tpl_users u ON t.tpl_designer_id = u.u_id
WHERE 1=1 $condition
)AS tmp1
LEFT JOIN tpl_users u1 ON tmp1.tpl_tester_id = u1.u_id	
STR;
    	$res = $_mysql_->customSql($sql);
    	if(empty($res))
    	{
    		$this->assign('empty',true);
    	}
    	else
    	{
    		$this->assign('empty',false);
    		$this->assign('result_data',$res);
    		
    	}
	}
}

?>